package com.bing.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.menu.WxMpGetSelfMenuInfoResult;

/**
 * @author 胡日亮
 * @create 2018-01-09 13:09
 */
@RestController
@RequestMapping("/menu")
public class WxMenuController {

    @Autowired
    private WxMpService wxMpService;

    @GetMapping("/create")
    public String crate() throws WxErrorException {
        WxMenu wxMenu = new WxMenu();
        WxMenuButton button = new WxMenuButton();
        button.setName("菜单");

        WxMenuButton button1 = new WxMenuButton();
        button1.setType(WxConsts.MenuButtonType.CLICK);
        button1.setName("今日歌曲");
        button1.setKey("Today_music");
        wxMenu.getButtons().add(button);

        wxMenu.getButtons().add(button1);

        WxMenuButton button2 = new WxMenuButton();
        button2.setType(WxConsts.MenuButtonType.VIEW);
        button2.setName("百度");
        button2.setUrl("http://www.easy.com");

        WxMenuButton button3 = new WxMenuButton();
        button3.setType(WxConsts.MenuButtonType.CLICK);
        button3.setName("Q一下");
        button3.setKey("GOOD");

        WxMenuButton button4 = new WxMenuButton();
        button4.setType(WxConsts.MenuButtonType.PIC_PHOTO_OR_ALBUM);
        button4.setName("拍照");
        button4.setKey("rselfmenu_1_1");

        button.getSubButtons().add(button2);
        button.getSubButtons().add(button3);
        button.getSubButtons().add(button4);

        //设置菜单
        System.out.println("创建结果" + wxMpService.getMenuService().menuCreate(wxMenu));
        return wxMpService.getMenuService().menuCreate(wxMenu);
    }

    /**
     * @Description 获取菜单信息
     * @Author 胡日亮
     * @Date 2018/1/19 16:06
     */
    @GetMapping(value = "getMenu")
    public WxMpGetSelfMenuInfoResult getMenu() throws WxErrorException {
        WxMpGetSelfMenuInfoResult selfMenuInfo = this.wxMpService.getMenuService().getSelfMenuInfo();
        System.out.println(selfMenuInfo);
        return selfMenuInfo;
    }

}